package com.qf.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zdl
 * @date 2025/1/11
 */
public class Demo2 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //通过Class对象 获取 有参构造方法
        Class<Animal> ac = Animal.class;
        Constructor<Animal> constructor = ac.getConstructor(null);
        Animal animal = constructor.newInstance();
        System.out.println(animal);

        //有参构造
        Constructor<Animal> constructor1 = ac.getConstructor(String.class);
        Animal animal1 = constructor1.newInstance("张三");
        System.out.println(animal1);

        Constructor<Animal> constructor2 = ac.getConstructor(String.class, int.class);
        Animal animal2 = constructor2.newInstance("李四", 22);
        System.out.println(animal2);

    }
}
